package org.zfes.snowier.cloud.zbss.widget.act.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.exceptions.ServiceLogicalException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectJoinHisService;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectJoinService;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectService;
import org.zfes.snowier.cloud.zbss.widget.act.dao.ActSubjectJoinMapper;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubject;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubjectJoin;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubjectJoinHis;

@Service
public class ActSubjectJoinServiceImpl implements IActSubjectJoinService{
 	@Autowired
	private ActSubjectJoinMapper actSubjectJoinMapper;
 	@Autowired
	private IActSubjectJoinHisService actSubjectJoinHisService;
	@Autowired
	private IActSubjectService actSubjectService;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void enterActSubject(ActSubjectJoin actSubjectJoin) {
		ZBeanUtil.setCreateInfo(actSubjectJoin);
		ZBeanUtil.validateBean(actSubjectJoin);
		
		ActSubject actSubject=actSubjectService.loadActSubjectById(actSubjectJoin.getSubjectId()).orElseThrow(()->new ServiceLogicalException("未查询到课堂数据"));
		//#0未进行， 1进行中，2已结束 -1删除#
		ZAssert.equals(actSubject.getStatus(), Byte.valueOf("1"), "当前课堂不是【进行中】状态，请提醒开启");
		
		
		actSubjectJoin.setJoinTime(ZDateUtil.newDate());
		//#0未进入, 1 已进入， 2已退出 #
		actSubjectJoin.setStatus(Byte.valueOf("1"));
		actSubjectJoinMapper.insertSelective(actSubjectJoin);
		
		//保存历史
		actSubjectJoinHisService.createActSubjectJoinHis(ActSubjectJoinHis.genEnter(actSubjectJoin));
	}
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void leftActSubject(Long subjectId,Long joinUserId) {
		
		actSubjectJoinMapper.deleteByUser(subjectId,joinUserId);
		
		actSubjectJoinHisService.createActSubjectJoinHis(ActSubjectJoinHis.genLeft(subjectId, joinUserId));
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ActSubjectJoin> loadActSubjectJoinById(Long id) {
		if(id!=null){
			return Optional.ofNullable(actSubjectJoinMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadActSubjectJoinDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(actSubjectJoinMapper.selectListCount(pm), actSubjectJoinMapper.selectMapListPage(pm));
		return ds;
	}
	
}